import java.util.Arrays;
import java.util.Scanner;

public class demo10 {
    //倍数问题
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int k=sc.nextInt();
        int[] arr=new int[n];
        for (int i = 0; i < n; i++) {
            arr[i]=sc.nextInt();
        }
        Arrays.sort(arr);
        int max=0;
        for (int m = n-1; m >=2 ; m--) {
            for (int i = m - 1; i >= 1; i--) {
                for (int j = i - 1; j >= 0; j--) {
                    int sum = arr[m] + arr[i] + arr[j];
                    if (sum % k == 0) {
                        max=Math.max(sum,max);
                    }
                    if(sum<max){
                        break;
                    }
                }
                if(arr[m]+arr[i]+arr[i-1]<max){
                    break;
                }
            }
            if(arr[m]+arr[m-1]+arr[m-2]<max){
                break;
            }
        }
        System.out.println(max);
    }
}
